package com.leps.nq.bu.controller;

import com.leps.nq.bu.entity.BuPlc;
import com.leps.nq.bu.service.IBuPlcService;
import com.leps.nq.bu.vo.PlcQueryModel;
import com.leps.nq.bu.vo.PlcVo;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * plc表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/bu/bu-plc")
@Api(tags = "plc管理")
public class BuPlcController {
    @Autowired
    private IBuPlcService plcService;

    @GetMapping("/forSelect")
    @ApiOperation("plc下拉列表")
    public R<List<BuPlc>> listForSelect(Integer stationId) {
        List<BuPlc> list = this.plcService.lambdaQuery().eq(stationId != null, BuPlc::getStationId, stationId).list();
        return RBuilder.ok(list);
    }

    @GetMapping
    @ApiOperation("plc列表")
    public R<PageVo<PlcVo>> list(PlcQueryModel queryModel) {
        PageVo<PlcVo> pageVo = this.plcService.pageByQueryModel(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询PLC信息")
    public R<BuPlc> getById(@PathVariable Integer id) {
        return RBuilder.ok(this.plcService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加plc")
    @Log(title = "PLC", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody BuPlc plc) {
        boolean b = this.plcService.save(plc);
        return RBuilder.ofBoolean(b);
    }

    @PutMapping
    @ApiOperation("更新plc")
    @Log(title = "PLC", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody BuPlc plc) {
        boolean b = this.plcService.updateById(plc);
        return RBuilder.ofBoolean(b);
    }

    @DeleteMapping
    @ApiOperation("删除plc")
    @Log(title = "PLC", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Integer> plcIds) {
        boolean b = this.plcService.removeBatchByIds(plcIds);
        return RBuilder.ofBoolean(b);
    }
}
